var api = require('../../utils/api.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: app.globalData.userInfo,
    upload: app.globalData.upload,
    num:235
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    api.post({
      url: 'wxapp/szufu/getRewardCord',
      data: {},
      success: data => {
       
         that.setData({
          num: data.data
        });
      }
    })
  },
  onpay: function (e) {
    var that = this;
    var openid = wx.getStorageSync('ooid')
    console.log('openid=' + openid);
    var userInfo = wx.getStorageSync('userInfo')
    var money = e.currentTarget.dataset.id;    //获取input初始值
    wx.request({
      method: 'GET',
      url: 'https://bn.heze123.cn/public/api/index.php/wxapp/pay/pay', //接口地址
      data: {
        'openid': openid,
        'money': money,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        // console.log('res==' + res.data);
        if (res.data) {

          out_trade_no: res.data['out_trade_no'];
          wx.requestPayment({
            'timeStamp': res.data['timeStamp'],
            'nonceStr': res.data['nonceStr'],
            'package': res.data['package'],
            'signType': 'MD5',
            'paySign': res.data['paySign'],
            'success': function (res2) {
              console.log('支付中');
              wx.request({
                method: 'POST',
                url: 'https://bn.heze123.cn/public/api/index.php/wxapp/szufu/saveReward',
                data: {
                  'wx_name': userInfo.nickName,
                  'money': money,
                  'wx_openid': openid,
                  'prepay_id': res.data['package']
                },
                header: { 'content-type': 'application/json' },
                success: function (res) {
                  wx.showToast({
                    title: '支付成功',
                    duration: 2000
                  })
                  setTimeout(function () {
                    wx.switchTab({
                      url: '/pages/index/index',
                    })
                  }, 2000)
                },
              })
            },
            'fail': function (res) {
              console.log(res);
            }
          })
        }
      },
      fail: function (res) {
        console.log('支付接口调用失败')
      }
    })



  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})